package com.example.jdai.jdai.ui;

import android.Manifest;
import android.app.Activity;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.adapter.MyPagerAdapter;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.rest.GetData;
import com.example.jdai.jdai.rest.RestApi;
import com.example.jdai.jdai.rest.SendCode;
import com.example.jdai.jdai.threeparty.UpdataService;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.Tools;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Response;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class Splash extends Activity implements BackRest.DoLong{

    @BindView(R.id.vp_welcome)
    ViewPager vpWelcome;
    @BindView(R.id.activity_splash)
    RelativeLayout activitySplash;
    List<View> views = new ArrayList<View>();
    int[] imgs = {R.drawable.wel_01, R.drawable.wel_02, R.drawable.wel_03};
    //int[] imgs = {R.drawable.wel_03};
    //Button[] btns = new Button[imgs.length];
    @BindView(R.id.ll_dots)
    LinearLayout llDots;
    int currect_dot = 0;
    @BindView(R.id.bt_enter)
    Button btEnter;
    final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!Tools.firstIn()){
            if(!"".equals(Tools.hasLogin())){
                MyApplication.backRest.getInfo(Splash.this);
//                if((!"0".equals(MyApplication.dataAll.data.get(DataAll.IS_COMMIT))&&
//                        (!"".equals(MyApplication.dataAll.data.get(DataAll.IS_COMMIT)))
//                )){
//                    startActivity(new Intent(this,ScActivity.class));
//                    finish();
//                    return;
//                }
//                else {
//                    startActivity(new Intent(this, MainActivity.class));
//                    finish();
//                    return;
//                }
                return;
            }
            startActivity(new Intent(this,RegistActivity.class));
            finish();
            return;
        }
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        LayoutInflater inflater = getLayoutInflater();
        //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dot);
        View view=null;
        for (int i : imgs) {
            view = inflater.inflate(R.layout.my_padapter, null);
            view.findViewById(R.id.iv_show_img).setBackgroundResource(i);
            //Button bt = new Button(Splash.this);
           // RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(30, 30);
           // rl.setMargins(20,20,20,20);
           // bt.setLayoutParams(rl);
           // bt.setBackgroundResource(R.drawable.dot);
           // llDots.addView(bt);
            views.add(view);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getSharedPreferences(Config.SP_NAME, Context.MODE_PRIVATE).edit().putBoolean(DataAll.FIRST_IN,false).commit();
                enter();
            }
        });
        vpWelcome.setAdapter(new MyPagerAdapter(views));
        //setDotSelected(currect_dot);
        vpWelcome.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //setDotSelected(position);
                //setCurrectDot_normal(currect_dot);
                //currect_dot = position;

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        PermissionGet();
    }

    void setCurrectDot_normal(int position) {
        if(llDots.getChildCount()>position)
        llDots.getChildAt(position).setBackgroundResource(R.drawable.dot);
    }

    void setDotSelected(int position) {
        if(llDots.getChildCount()>position) {
            llDots.getChildAt(position).setBackgroundResource(R.drawable.dot_select);
            if (position == (imgs.length - 1)) {
                btEnter.setVisibility(View.VISIBLE);
            } else {
                if (btEnter.getVisibility() == View.VISIBLE)
                    btEnter.setVisibility(View.GONE);
            }
        }
    }
    void enter() {
        if(!"".equals(Tools.hasLogin())){
           // Log.v("sss",Tools.hasLogin());
            startActivity(new Intent(this,MainActivity.class));
            finish();
            return;
        }
        else {
            startActivity(new Intent(this, RegistActivity.class));
            finish();
        }
     }

    @Override
    protected void onDestroy() {
        views=null;
        super.onDestroy();
    }

    @Override
    public void doDone(int mark, Map str) {


        switch (mark) {
            case -1:
                startActivity(new Intent(this, NetErrorActivity.class));
                finish();
                break;
            default:
                if ((!"0".equals(MyApplication.dataAll.data.get(DataAll.IS_COMMIT)) &&
                        (!"".equals(MyApplication.dataAll.data.get(DataAll.IS_COMMIT)))
                )) {
                    startActivity(new Intent(this, ScActivity.class));
                    finish();
                    return;
                } else {
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                    return;
                }
        }


    }
    //获得权限
    void PermissionGet()
    {
        //int REQUEST_EXTERNAL_STORAGE = 1;
        String[] PERMISSIONS_STORAGE = {
                Manifest.permission.READ_SMS,
                Manifest.permission.RECEIVE_SMS,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,


        };
        boolean permission3=Tools.selfPermissionGranted(Splash.this, Manifest.permission.READ_EXTERNAL_STORAGE);
        boolean permission = Tools.selfPermissionGranted(Splash.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        boolean permission2 = Tools.selfPermissionGranted(Splash.this, Manifest.permission.READ_CONTACTS);
        if (permission&&permission2&&permission3) {
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    if(!Tools.GetContacts()){
//                        final AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);
//                        //    设置Title的图标
//                        builder.setIcon(R.drawable.logo);
//                        builder.setTitle("缺少权限");
//                        builder.setCancelable(false);
//                        builder.setMessage("软件部分功能依赖通讯录，请打开权限！");
//                        //    设置一个PositiveButton
//                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                            @Override
//                            public void onClick(DialogInterface dialog, int which) {
//                                android.os.Process.killProcess(android.os.Process.myPid());
//
//                            }
//                        });
//                        Tools.postToMain(new Runnable() {
//                            @Override
//                            public void run() {
//                                builder.show();
//                            }
//                        });
//                    }
//                }
//            }).start();
        }
        else {

            ActivityCompat.requestPermissions(
                    Splash.this,
                    PERMISSIONS_STORAGE,
                    REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS
            );
            if(Config.ISDEBUG){
                Log.v("splpr",permission+";"+permission2+";"+permission3);
            }

        }
        // Tools.GetContacts();

        //Log.v("ddd","sssss");
    }

}
